﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainPageViewModel.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Main page view model
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight.MVVM.ViewModels
{
    #region Using Directives

    using System.Reflection;
    using System.Windows.Browser;
    using GalaSoft.MvvmLight.Messaging;
    using Globals;
    using Logging;
    using Messages;
    using StreetCareAPI;
    using UserControls;

    #endregion

    /// <summary>
    /// Main page view model
    /// </summary>
    public class MainPageViewModel : BaseViewModel
    {
        /// <summary>
        /// Handler for new report window
        /// </summary>
        private readonly CreateNewReportChildControl createNewReportWindow;

        /// <summary>
        /// Handler for report detail window
        /// </summary>
        private readonly ReportDetailChildControl reportDetailWindow;

        #region Class Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="MainPageViewModel"/> class.
        /// </summary>
        public MainPageViewModel()
        {
            this.createNewReportWindow = new CreateNewReportChildControl();
            this.reportDetailWindow = new ReportDetailChildControl();

            if (HtmlPage.IsEnabled)
            {
                var customerID = Parameters.CustomerID;
                ApiClient.GetCustomerByIDCompleted += this.GetCustomerByIDCompleted;
                ApiClient.GetCustomerByIDAsync(customerID);

                Messenger.Default.Register<NewReportMessage>(this, this.NewReport);
                Messenger.Default.Register<NewCommentCompletedMessage>(this, this.NewCommentCompleted);
                Messenger.Default.Register<NewReportCompletedMessage>(this, this.NewReportCompleted);
                Messenger.Default.Register<OpenReportMessage>(this, this.OpenReport);
            }
        }

        #endregion

        #region Event Methods

        /// <summary>
        /// Handles the get customer by ID completed event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Spenta.StreetCare.Silverlight.StreetCareAPI.GetCustomerByIDCompletedEventArgs"/> instance containing the event data.</param>
        public void GetCustomerByIDCompleted(object sender, GetCustomerByIDCompletedEventArgs e)
        {
            try
            {
                if (e.Result.IsFault)
                {
                    LogHelper.Logger.WriteError("Error getting customer", e.Result.Fault.Exception);
                    return;
                }
            }
            catch (TargetInvocationException ex)
            {
                LogHelper.Logger.WriteError("Error getting customer", ex.InnerException ?? ex);
                return;
            }

            Messenger.Default.Send(e.Result.Customer);
        }

        /// <summary>
        /// Handles the new report event.
        /// </summary>
        /// <param name="message">The message.</param>
        public void NewReport(NewReportMessage message)
        {
            this.createNewReportWindow.Show();
        }

        /// <summary>
        /// Handles the new report completed event.
        /// </summary>
        /// <param name="message">The message.</param>
        public void NewReportCompleted(NewReportCompletedMessage message)
        {
            if (message.IsError)
            {
                // TODO
                return;
            }

            this.createNewReportWindow.Close();
        }

        /// <summary>
        /// Handles the new report completed event.
        /// </summary>
        /// <param name="message">The message.</param>
        public void NewCommentCompleted(NewCommentCompletedMessage message)
        {
            if (message.IsError)
            {
                // TODO
                return;
            }

            this.reportDetailWindow.Close();
        }

        /// <summary>
        /// Handles the open report event.
        /// </summary>
        /// <param name="message">The message.</param>
        public void OpenReport(OpenReportMessage message)
        {
            this.reportDetailWindow.Show();
        }

        #endregion
    }
}
